require('./index.css');

import _mm from 'util/mm.js';
import _tool from 'util/tool.js';
import _all from 'service/all-service.js';


const page ={
    init() {
        this.bindEvent();
        this.initValidate();
    },
    submitQueryInfo() {
        const flag = $('#queryForm').valid();
        if (!flag) {
            return;
        }
        const _this = this;
        let formData = $('#queryForm').serializeArray();
        formData = _mm.serializeJsonObj(formData);
        console.log(formData);
        _all.sendCartList(JSON.stringify(formData), res => {
            _tool.msgShakeLayer('Successful submission！');
            $('.inquiry-form').hide();
        }, err => {
            _tool.msgShakeLayer(err.message);
        });
    },
    bindEvent() {
        const _this = this;
        $('.btn-inquiry').on('click', function () {
            $('.inquiry-form').toggle();
        });
        $('#send').on('click', function () {
            _this.submitQueryInfo();
        })
    },
    initValidate() {
        jQuery.validator.addMethod("isphoneNum", function (value, element) {
            var length = value.length;
            var mobile = /^1[3|5|8]{1}[0-9]{9}$/;
            return this.optional(element) || (length == 11 && mobile.test(value));
        }, "请正确填写您的手机号码");
        $('#queryForm').validate({
            errorPlacement: function(error, element) {
                $(element).siblings('.error-tip').html(error);
            },
            ignore: [],
            rules: {
                name: "required",
                country: "required",
                message: "required",
                email: {
                    required: true,
                    email: true
                },
                phone: {
                    isphoneNum: true
                }
            },
            messages: {
                name: "Name",
                country: "Country",
                message: "Message",
                email: {
                    required: 'Email',
                    email: 'Email'
                },
                phone: {
                    isphoneNum: 'Phone'
                }
            }
        });
    }
};

module.export = page.init();